package il.ac.technion.wiki.api.search;


/**
 * Represents a search expression that must contain <i>both</i> specified
 * search expressions.
 */
public class AndSearchExpression implements ISearchExpression {
	@Override
	public int numberOfOperands() {
		return 2;
	}

	@Override
	public String toString() {
		return "{"+left.toString()+"} <--AND--> {"+right.toString()+"}";
	}

	/**
	 * Creates a new AndSearchExpression.
	 * @param left	The left subtree of the search tree.
	 * @param right	The right subtree of the search tree.
	 */
	public AndSearchExpression(ISearchExpression left, ISearchExpression right) {
		this.left = left;
		this.right = right;
	}
	
	/**
	 * Allows users to easily construct a tree reflecting a long series of AND
	 * predicates, e.g.: "Sally" AND "Joe" AND "Mary" AND "Me"
	 * @param tgt	The expression to append to.
	 * @param expr	The expression to append.
	 * @return		An appropriate query tree.
	 */
	public static ISearchExpression append(ISearchExpression tgt, ISearchExpression expr) {
		if (tgt == null || tgt.getClass() == NopSearchExpression.class)	// optimized to ignore nop. creates simpler expressions...
			return expr;
		return new AndSearchExpression(tgt, expr);
	}
	
	/**
	 * The two sub-expressions that must appear to match this search expression.
	 */
	public final ISearchExpression left, right;
}
